local OUTPUT_STRING = "%s 战斗中拾取。"

local hexColors = { WTF = "|cffa0a0a0" }
for k, v in pairs(RAID_CLASS_COLORS) do
	hexColors[k] = "|cff" .. string.format("%02x%02x%02x", v.r * 255, v.g * 255, v.b * 255)
end
local coloredName = setmetatable({}, {__index =
	function(self, key)
		if type(key) == "nil" then return nil end
		local class = select(2, UnitClass(key)) or "WTF"
		self[key] = hexColors[class]  .. key .. "|r"
		return self[key]
	end
})

LootWhore = AceLibrary("AceAddon-2.0"):new("AceDB-2.0", "AceEvent-2.0", "AceConsole-2.0")
local sink = LibStub("LibSink-2.0")
sink:Embed(LootWhore)

function LootWhore:OnInitialize()
	self:RegisterDB("LootWhoreDB")
	self:RegisterDefaults("profile", {
		sink20OutputSink = "Channel",
		sink20ScrollArea = "Say",
	})
	self:SetSinkStorage(self.db.profile)
	
	local options = {
		type = "group",
		name = "LootWhore",
		desc = "LootWhore options.",
	}
	options.args = self:GetSinkAce2OptionsDataTable()
	
	self:RegisterChatCommand("/lootwhore", options, "LOOTWHORE")
end

function LootWhore:OnEnable()
	self:RegisterEvent("CHAT_MSG_ADDON")
end

local function inGroup(player)
	return UnitInRaid(player) or UnitInParty(player) and true or nil
end

function LootWhore:CHAT_MSG_ADDON(prefix, message, distribution, sender)
	if prefix ~= "LOOT_OPENED" or not InCombatLockdown() or not inGroup(sender) then return end
	-- Find the zone of the person and check it against our own zone, if they're
	-- different, don't pour anything.
	if UnitInRaid("player") then
		-- Not sure how to find the zone info of a party member yet, only raid
		-- members.
		for i = 1, GetNumRaidMembers() do
			local name, _, _, _, _, _, zone = GetRaidRosterInfo(i)
			if name == sender then
				if zone ~= GetRealZoneText() then return
				else break end
			end
		end
	end
	self:Pour(OUTPUT_STRING:format(coloredName[sender]))
end

